/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transas.zornav.data;

import java.text.NumberFormat;
import java.util.Locale;

/**
 *
 * @author amalikov
 */
public class DegMinSecFormatter implements Formatter {

	public DegMinSecFormatter(String pos, String neg) {
		this.pos = pos;
		this.neg = neg;
		nf = NumberFormat.getNumberInstance(Locale.US);
		nf.setGroupingUsed(false);
		nf.setMinimumIntegerDigits(2);
		nf.setMinimumFractionDigits(0);
		nf.setMaximumFractionDigits(2);
	}
	
	@Override
	public String get(double value) {
		double absvalue = Math.abs(value);
		
		int deg = (int)absvalue;
		int min = (int)((absvalue - deg) * 60);
		double sec = ((absvalue - deg) * 60 - min) * 60;
			
		StringBuilder sb = new StringBuilder();
		sb.append(value >= 0 ? pos : neg);
		nf.setMinimumFractionDigits(0);
		sb.append(nf.format(deg)).append("°");
		sb.append(nf.format(min)).append("'");
		nf.setMinimumFractionDigits(2);
		sb.append(nf.format(sec)).append("\"");
		
		return sb.toString();
	}
	
	private NumberFormat nf;
	private final String pos;
	private final String neg;
}
